/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.controller;

import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.purpose.domain.JobsHazardLibrary;
import com.koron.purpose.service.api.JobsHazardLibraryService;
import com.koron.purpose.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;

@RestController
@Api(tags = "jobsHazardLibrary", description = "岗位危害因素模板库")
@RequestMapping("/jobsHazardLibrary")
public class JobsHazardLibraryController {

    @Autowired
    private JobsHazardLibraryService service;

    @ApiOperation("保存岗位危害因素模板库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id,新增不传，修改必传",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "危害因素名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "description", value = "描述信息",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "measures", value = "防控措施",  dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/save.htm", method = {RequestMethod.POST})
    @Log(title = "新增、修改岗位危害因素模板库", businessModule = "岗位危害因素模板", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<Integer> save(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore JobsHazardLibrary library) {
        if (null == user) {
            return MessageBean.create(BusinessCode.NO_LOGIN.getCode(), BusinessCode.NO_LOGIN.getDescription(), Integer.class);
        }
        library.setUpdateTime(new Date());
        library.setUpdateUser(user.getCode());
        library.setUpdateUserName(user.getName());
        return service.save(library);
    }

    @ApiOperation("删除岗位危害因素模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id,新增不传，修改必传", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/delete.htm", method = {RequestMethod.POST})
    @Log(title = "删除岗位危害因素模板", businessModule = "岗位危害因素模板", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<Integer> delete(@RequestParam final String id) {
        return service.delete(id);
    }

    @ApiOperation("获取岗位危害因素模板库详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/detail.htm", method = {RequestMethod.GET})
    public MessageBean<JobsHazardLibrary> detail(@RequestParam final String id) {
        return service.queryOne(id);
    }

    @ApiOperation("获取岗位危害因素模板库列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "公司名称",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "危害因素名称",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "description", value = "描述信息",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "measures", value = "防控措施",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页",  dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/list.htm", method = {RequestMethod.GET})
    public MessageBean<DataBean<JobsHazardLibrary>> list(@RequestParam(required = false) final String corpName, @RequestParam(required = false) final String name, @RequestParam(required = false) final String description,
                                                         @RequestParam(required = false) final String measures, @RequestParam(required = false) final Integer page,
                                                         @RequestParam(required = false) Integer pageSize,@RequestParam(required = false) String corpCode) {

        Integer start = null;
        if (null != page && null != pageSize) {
            start = (page - 1) * pageSize;
        }
        MessageBean<DataBean<JobsHazardLibrary>> msg = service.queryByContion(corpCode,corpName, name, description, measures, start, pageSize);
        if (null != msg.getData())
            msg.getData().setPageNo(page);
        return msg;
    }
}
